package com.db.service.impl;

import com.db.Dao.PersonDao;
import com.db.Dao.impl.PersonDaoImpl;
import com.db.entity.Person;
import com.db.service.PersonService;
import com.db.util.DButils;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class PersonServiceImpl implements PersonService {
    @Override
    public List<Person> RetrieveAll(Integer userid) {
        Connection conn = null;
        List<Person>list = new ArrayList<>();

        try{
            conn = DButils.getConnection();
            conn.setAutoCommit(false);

            PersonDao pd = new PersonDaoImpl();
             list = pd.searchAll(userid);


            conn.commit();
        }catch( Exception e ){
            e.printStackTrace();
            if( conn != null ){
                try {
                    conn.rollback();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }
            throw new RuntimeException(e);
        }finally{
            DButils.close(conn,null,null);
        }
        return list;
    }

    @Override
    public Person retrievePersonByNameMobile(Person person,Integer userid) {
        Connection conn = null;
        Person person1 =null;
        try{
            conn = DButils.getConnection();
            conn.setAutoCommit(false);
            PersonDao pd = new PersonDaoImpl();
            person1 = pd.searchByNameMobile(person,userid);

            conn.commit();
        }catch(Exception e){
            e.printStackTrace();
            if( conn != null ){
                try {
                    conn.rollback();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }
            throw new RuntimeException(e);

        }finally{
            DButils.close(conn,null,null);
        }

        return person1;
    }

    @Override
    public void addPerson(Person person) {
        Connection conn = null;

        try{
            conn = DButils.getConnection();

            conn.setAutoCommit(false);
            PersonDao pd = new PersonDaoImpl();
            pd.addPerson(person);

            conn.commit();
        }catch(Exception e){
            e.printStackTrace();
            if(conn != null){
                try {
                    conn.rollback();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }

            }
            throw new RuntimeException(e);

        }finally{
            DButils.close(conn,null,null);
        }

    }

    @Override
    public void removePerson(Integer personId) {
        Connection conn = null;

        try{
            conn = DButils.getConnection();
            conn.setAutoCommit(false);

            PersonDao pd = new PersonDaoImpl();
            pd.deletePersonById(personId);

            conn.commit();
        }catch(Exception e){
            e.printStackTrace();
            if(conn != null){
                try { conn.rollback(); } catch (SQLException e1) { e1.printStackTrace(); }
            }
            throw new RuntimeException(e);

        }finally {
            DButils.close(conn,null,null);

        }
    }

    @Override
    public Person showPerson(Integer personId,Integer userid) {
        Connection conn = null;
        Person person = null;

        try{
            conn = DButils.getConnection();
            conn.setAutoCommit( false );
            PersonDao pd = new PersonDaoImpl();
            person = pd.selectPersonById(personId,userid);

            conn.commit();
        }catch(Exception e){
            e.printStackTrace();
            if( conn != null){ try{conn.rollback();}catch(Exception e1){e1.printStackTrace();}}
            throw new RuntimeException(e);

        }finally {
            DButils.close(conn,null,null);
        }

        return person;
    }

    @Override
    public void changePerson(Person person) {
        Connection conn = null;

        try{
            conn = DButils.getConnection();
            conn.setAutoCommit(false);
            PersonDao pd = new PersonDaoImpl();
            pd.updatePerson(person);

            conn.commit();
        }catch(Exception e){
            e.printStackTrace();
            if(conn != null){
                try { conn.rollback(); } catch (SQLException throwables) { throwables.printStackTrace(); }
            }
            throw new RuntimeException(e);
        }finally {
            DButils.close(conn,null,null);
        }

    }

    @Override
    public List<Person> retrieveLimit(Integer pageIndex,Integer userid) {

        Connection conn = null;
        List<Person>list = new ArrayList<>();

        try{
            conn = DButils.getConnection();
            conn.setAutoCommit(false);

            PersonDao pd = new PersonDaoImpl();
            list = pd.searchlimit(pageIndex,userid);


            conn.commit();
        }catch( Exception e ){
            e.printStackTrace();
            if( conn != null ){
                try {
                    conn.rollback();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }
            throw new RuntimeException(e);
        }finally{
            DButils.close(conn,null,null);
        }
        return list;
    }

    @Override
    public List<Person> retrieveByName(Integer pageIndex, String personName,Integer userid) {
        Connection conn = null;
        List<Person>list = new ArrayList<>();

        try{
            conn = DButils.getConnection();
            conn.setAutoCommit(false);

            PersonDao pd = new PersonDaoImpl();
            list = pd.searchByName(pageIndex,personName,userid);


            conn.commit();
        }catch( Exception e ){
            e.printStackTrace();
            if( conn != null ){
                try {
                    conn.rollback();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }
            throw new RuntimeException(e);
        }finally{
            DButils.close(conn,null,null);
        }
        return list;
    }

    @Override
    public List<Person> retrieveByMobile(Integer pageIndex, String mobile,Integer userid) {
        Connection conn = null;
        List<Person>list = new ArrayList<>();

        try{
            conn = DButils.getConnection();
            conn.setAutoCommit(false);

            PersonDao pd = new PersonDaoImpl();
            list = pd.searchByMobile(pageIndex,mobile,userid);


            conn.commit();
        }catch( Exception e ){
            e.printStackTrace();
            if( conn != null ){
                try {
                    conn.rollback();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }
            throw new RuntimeException(e);
        }finally{
            DButils.close(conn,null,null);
        }
        return list;
    }

    @Override
    public List<Person> retrieveByAddress(Integer pageIndex, String address,Integer userid) {
        Connection conn = null;
        List<Person>list = new ArrayList<>();

        try{
            conn = DButils.getConnection();
            conn.setAutoCommit(false);

            PersonDao pd = new PersonDaoImpl();
            list = pd.searchByAddress(pageIndex,address,userid);


            conn.commit();
        }catch( Exception e ){
            e.printStackTrace();
            if( conn != null ){
                try {
                    conn.rollback();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }
            throw new RuntimeException(e);
        }finally{
            DButils.close(conn,null,null);
        }
        return list;
    }

    @Override
    public List<Person> retrieveByNameNo(String personName,Integer userid) {
        Connection conn = null;
        List<Person>list = new ArrayList<>();

        try{
            conn = DButils.getConnection();
            conn.setAutoCommit(false);

            PersonDao pd = new PersonDaoImpl();
            list = pd.searchByNameNo(personName,userid);


            conn.commit();
        }catch( Exception e ){
            e.printStackTrace();
            if( conn != null ){
                try {
                    conn.rollback();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }
            throw new RuntimeException(e);
        }finally{
            DButils.close(conn,null,null);
        }
        return list;
    }

    @Override
    public List<Person> retrieveByMobileNo(String mobile,Integer userid) {
        Connection conn = null;
        List<Person>list = new ArrayList<>();

        try{
            conn = DButils.getConnection();
            conn.setAutoCommit(false);

            PersonDao pd = new PersonDaoImpl();
            list = pd.searchByMobileNo(mobile,userid);


            conn.commit();
        }catch( Exception e ){
            e.printStackTrace();
            if( conn != null ){
                try {
                    conn.rollback();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }
            throw new RuntimeException(e);
        }finally{
            DButils.close(conn,null,null);
        }
        return list;
    }

    @Override
    public List<Person> retrieveByAddressNo(String address,Integer userid) {
        Connection conn = null;
        List<Person>list = new ArrayList<>();

        try{
            conn = DButils.getConnection();
            conn.setAutoCommit(false);

            PersonDao pd = new PersonDaoImpl();
            list = pd.searchByAddressNo(address,userid);


            conn.commit();
        }catch( Exception e ){
            e.printStackTrace();
            if( conn != null ){
                try {
                    conn.rollback();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }
            throw new RuntimeException(e);
        }finally{
            DButils.close(conn,null,null);
        }
        return list;
    }
}
